#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_Rs485Protocol.h"
#include "mg_Rotate.h"
/* widgets:
Layer_SetRGB
Background_RGB
Container_RGB
Icon_RGB_Pointer
Meter_RGB_Xuanzhuan
Icon_RGB_Ring
Icon_RGB_Botton
Button_SetRGB
*/
//设置RGB颜色深度V
void Set_RGBcolor_V(float V);

static ITUBackground* Background_RGB;
static ITUMeter* Meter_RGB_Xuanzhuan;
static uint8_t R, G, B;
static float H, S, V;
static int MeterValue = 0, MeterValue_L = 0;
bool RGBOnEnter(ITUWidget* widget, char* param)
{
	if (!Background_RGB)//首次加载
	{
		Background_RGB = ituSceneFindWidget(&theScene, "Background_RGB");
		assert(Background_RGB);
		Meter_RGB_Xuanzhuan = ituSceneFindWidget(&theScene, "Meter_RGB_Xuanzhuan");
		assert(Meter_RGB_Xuanzhuan);
	}
	R = TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[0];
	G = TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[1];
	B = TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[2];
	RGB_2_HSB(R, G, B, &H, &S, &V);
	ituMeterSetValue(Meter_RGB_Xuanzhuan, Get_Angle_By_RGBcolor(R, G, B));
	MeterValue = Meter_RGB_Xuanzhuan->value;
	MeterValue_L = MeterValue;
	ituWidgetSetColor(Background_RGB, 0xFF, R, G, B);
	Set_Rotate_Block(30);//旋转步进
	return true;
}

bool RGBOnLeave(ITUWidget* widget, char* param)
{
	return true;
}

bool RGBOnTimer(ITUWidget* widget, char* param)
{
	static int RGBAngle;
	static uint32_t SendEndTime = Rotate_SendCmd_Time, SendTime = 0;

	if (Action_Cmd.User_Action == Clockwise_Rotation)//顺时针旋转
	{
		MeterValue += 3;
		if (MeterValue >= 360)MeterValue -= 360;
		ituMeterSetValue(Meter_RGB_Xuanzhuan, MeterValue);//设置Meter百分比值
		MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
		Clear_User_Action();
	}
	else if (Action_Cmd.User_Action == Anticlockwise_Rotation)//逆时针旋转
	{
		MeterValue -= 3;
		if (MeterValue <= 0)MeterValue += 360;
		ituMeterSetValue(Meter_RGB_Xuanzhuan, MeterValue);//设置Meter百分比值
		MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
		Clear_User_Action();
	}
	else
	{
		Clear_User_Action();
	}

	MeterValue = Meter_RGB_Xuanzhuan->value;
	//结束操作设备状态执行
	if (MeterValue == MeterValue_L)
	{
		if (SendEndTime <= Rotate_SendCmd_Time)
			SendEndTime++;
		//停止操作0.6s后发送执行报文
		if (SendEndTime == Rotate_SendCmd_Time)
		{
			SendTime = 0;
			RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
			MessageFlag[MSG_GET_DEVICE_STATE_EN] = ON;//允许10s自动获取状态
		}
	}
	else
	{
		SendEndTime = 0;
		RGBAngle = 360 - MeterValue;
		RGBAngle -= 90;
		if (RGBAngle < 0)RGBAngle += 360;
		Get_RGBcolor_By_Angle(RGBAngle);
		ituWidgetSetColor(Background_RGB, 0xFF, R, G, B);

		TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[0] = R;
		TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[1] = G;
		TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[2] = B;
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 0x0b;//命令调色
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[0] = R;
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[1] = G;
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[2] = B;
	}
	//用MSG_GET_DEVICE_STATE_EN标记来表示当前是否在旋转动作中
	if (MessageFlag[MSG_GET_DEVICE_STATE_EN] == OFF)
	{
		if (SendTime < Rotating_SendCmd_Time)
			SendTime++;
		if (SendTime == Rotating_SendCmd_Time)//0.495s发送一次命令
		{
			SendTime = 0;
			RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
		}
	}
	//设备状态更新
	if (MessageFlag[MSG_GUI_STATE_UPDATE] == ON)
	{
		R = TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[0];
		G = TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[1];
		B = TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[2];
		ituMeterSetValue(Meter_RGB_Xuanzhuan, Get_Angle_By_RGBcolor(R, G, B));
		ituWidgetSetColor(Background_RGB, 0xFF, R, G, B);
		MeterValue = Meter_RGB_Xuanzhuan->value;//防止设备循环上报
		MessageFlag[MSG_GUI_STATE_UPDATE] = OFF;
	}
	MeterValue_L = MeterValue;
	return true;
}

bool Meter_RGB_Xuanzhuan_Changed(ITUWidget* widget, char* param)
{
	return true;
}

bool Button_SetRGB_Press(ITUWidget* widget, char* param)
{
	//发送指令
	TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[0] = R;
	TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[1] = G;
	TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[2] = B;
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 0x0b;//命令调色
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[0] = R;
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[1] = G;
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[2] = B;
	return true;
}

//根据角度(H)获取 R,G,B
void Get_RGBcolor_By_Angle(uint16_t Angle)
{
	if (Angle == 0 || Angle == 360)//纯红
	{
		R = 255;
		G = 0;
		B = 0;
	}
	else if (Angle == 120)//纯蓝
	{
		R = 0;
		G = 0;
		B = 255;
	}
	else if (Angle == 240)//纯绿
	{
		R = 0;
		G = 255;
		B = 0;
	}
	else if (Angle <= 60)
	{
		R = 255;
		G = 0;
		B = 255 * ((float)(Angle) / 60);
	}
	else if (Angle < 120)
	{
		R = 255 * ((float)(120 - Angle) / 60);
		G = 0;
		B = 255;
	}
	else if (Angle <= 180)
	{
		R = 0;
		G = 255 * ((float)(Angle - 120) / 60);
		B = 255;
	}
	else if (Angle < 240)
	{
		R = 0;
		G = 255;
		B = 255 * ((float)(240 - Angle) / 60);
	}
	else if (Angle <= 300)
	{
		R = 255 * ((float)(Angle - 240) / 60);
		G = 255;
		B = 0;
	}
	else if (Angle < 360)
	{
		R = 255;
		G = 255 * ((float)(360 - Angle) / 60);
		B = 0;
	}
	Set_RGBcolor_V(V);
}
//根据明亮程度(B)获取R,G,B
void Get_RGBcolor_By_Brightness(uint8_t Brightness)
{
	uint16_t Angle;
	Angle = Get_Angle_By_RGBcolor(R, G, B);

}
//根据R,G,B获取角度(H)
uint16_t Get_Angle_By_RGBcolor(uint8_t rgbR, uint8_t rgbG, uint8_t rgbB)
{
	int16_t Angle;
	uint8_t MaxValue, MinValue;

	//获取RGB三个中的最大值
	MaxValue = (rgbR >= rgbG) ? rgbR : rgbG;
	MaxValue = (MaxValue >= rgbB) ? MaxValue : rgbB;
	//获取RGB三个中的最小值
	MinValue = (rgbR <= rgbG) ? rgbR : rgbG;
	MinValue = (MinValue <= rgbB) ? MinValue : rgbB;

	if (MaxValue == MinValue)return 270;

	if (MaxValue == rgbR && rgbG >= rgbB) {
		Angle = (rgbG - rgbB) * 60 / (MaxValue - MinValue) + 0;
	}
	else if (MaxValue == rgbR && rgbG < rgbB) {
		Angle = (rgbG - rgbB) * 60 / (MaxValue - MinValue) + 360;
	}
	else if (MaxValue == rgbG) {
		Angle = (rgbB - rgbR) * 60 / (MaxValue - MinValue) + 120;
	}
	else if (MaxValue == rgbB) {
		Angle = (rgbR - rgbG) * 60 / (MaxValue - MinValue) + 240;
	}

	Angle -= 90;
	if (Angle < 0)Angle += 360;
	return (uint16_t)Angle;
}
//根据R,G,B获取明亮程度(B)
uint8_t Get_Brightness_By_RGBcolor(uint8_t rgbR, uint8_t rgbG, uint8_t rgbB)
{
	int8_t Brightness;
	float MaxValue, MinValue;
	float Brightness_f;
	//获取RGB三个中的最大值
	MaxValue = (rgbR >= rgbG) ? rgbR : rgbG;
	MaxValue = (MaxValue >= rgbB) ? MaxValue : rgbB;
	//获取RGB三个中的最小值
	MinValue = (rgbR <= rgbG) ? rgbR : rgbG;
	MinValue = (MinValue <= rgbB) ? MinValue : rgbB;

	Brightness_f = MaxValue * 100 / 255;
	Brightness = (Brightness_f + 0.5);
	return Brightness;
}

//RGB参数转换为HSB
void RGB_2_HSB(uint8_t rgbR, uint8_t rgbG, uint8_t rgbB, float* hsbH, float* hsbS, float* hsbB)
{
	float MaxValue, MinValue;
	float Temp;

	//获取RGB三个中的最大值
	MaxValue = (rgbR >= rgbG) ? rgbR : rgbG;
	MaxValue = (MaxValue >= rgbB) ? MaxValue : rgbB;
	//获取RGB三个中的最小值
	MinValue = (rgbR <= rgbG) ? rgbR : rgbG;
	MinValue = (MinValue <= rgbB) ? MinValue : rgbB;

	if (MaxValue == MinValue)
	{
		*hsbB = MaxValue / 255;
		*hsbS = 0;
		*hsbH = 0;
	}
	else
	{
		*hsbB = MaxValue / 255;
		*hsbS = MaxValue == 0 ? 0 : (MaxValue - MinValue) / MaxValue;

		if (MaxValue == rgbR && rgbG >= rgbB) {
			Temp = (rgbG - rgbB) * 60 / (MaxValue - MinValue) + 0;
		}
		else if (MaxValue == rgbR && rgbG < rgbB) {
			Temp = (rgbG - rgbB) * 60 / (MaxValue - MinValue) + 360;
		}
		else if (MaxValue == rgbG) {
			Temp = (rgbB - rgbR) * 60 / (MaxValue - MinValue) + 120;
		}
		else if (MaxValue == rgbB) {
			Temp = (rgbR - rgbG) * 60 / (MaxValue - MinValue) + 240;
		}
		*hsbH = Temp;
	}

}
//HSB参数转换为RGB
void HSB_2_RGB(float hsbH, float hsbS, float hsbB, uint8_t* rgbR, uint8_t* rgbG, uint8_t* rgbB)
{
	uint8_t Temp;
	float r, g, b, f, p, q, t;
	int i;
	//Assert((hsbH >= 0) && (hsbH <= 360), "HSB value H is error!");
	//Assert((hsbS >= 0) && (hsbS <= 1), "HSB value S is error!");
	//Assert((hsbB >= 0) && (hsbB <= 1), "HSB value B is error!");

	i = (((int)hsbH / 60) % 6);
	f = (hsbH / 60) - i;
	p = hsbB * (1 - hsbS);
	q = hsbB * (1 - f * hsbS);
	t = hsbB * (1 - (1 - f) * hsbS);
	switch (i) {
	case 0:
		r = hsbB;
		g = t;
		b = p;
		break;
	case 1:
		r = q;
		g = hsbB;
		b = p;
		break;
	case 2:
		r = p;
		g = hsbB;
		b = t;
		break;
	case 3:
		r = p;
		g = q;
		b = hsbB;
		break;
	case 4:
		r = t;
		g = p;
		b = hsbB;
		break;
	case 5:
		r = hsbB;
		g = p;
		b = q;
		break;
	default:
		break;
	}

	*rgbR = r * 255;
	*rgbG = g * 255;
	*rgbB = b * 255;
}
//设置RGB颜色深度V
void Set_RGBcolor_V(float V)
{
	R *= V;
	G *= V;
	B *= V;
}